package zd.cornermemory.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import zd.cornermemory.R;
import zd.cornermemory.fragment.record.RecordFragment;
import zd.cornermemory.fragment.record.StatisticsFragment;
import zd.cornermemory.fragment.record.TimeFragment;
import zd.cornermemory.utils.SpKey;

public class CjActivity extends BaseActivity implements View.OnClickListener{

    private TimeFragment mFrag1;
    private StatisticsFragment mFrag2;
    private RecordFragment mFrag3;
    private Button txt_tj;

    private TextView tab1;
    private TextView tab2;
    private TextView tab3;

    private int currentPos;
    private String titleStr;

    private int currentTab = 0;

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mFrag1 != null) {
                mFrag1.initData();
            }
            if (mFrag2 != null) {
                mFrag2.initData();
            }
            if (mFrag3 != null) {
                mFrag3.initData();
            }
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        IntentFilter filter = new IntentFilter();
        filter.addAction(SpKey.UPDATE_CJ);
        filter.addAction(SpKey.DELETE_CJ);
        registerReceiver(receiver, filter);
    }

    public int getCurrentTab() {
        return currentTab;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cj);
        txt_tj = findViewById(R.id.txt_tj);
        tab1 = findViewById(R.id.tab1);
        tab2 = findViewById(R.id.tab2);
        tab3 = findViewById(R.id.tab3);
        txt_tj.setOnClickListener(this);
        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);
        currentPos = getIntent().getExtras().getInt("currentPos");
        String[] scr = getResources().getStringArray(R.array.scr);
        titleStr = scr[currentPos];
        selectTab(0);
        setBackImage();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.txt_tj:
                Intent intent = new Intent(mContext, GraphicActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("currentPos",currentPos);
                bundle.putString("title",titleStr);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.tab1:
                selectTab(0);
                currentTab = 0;
                break;
            case R.id.tab2:
                selectTab(1);
                currentTab = 1;
                break;
            case R.id.tab3:
                selectTab(2);
                currentTab = 2;
                break;
        }
    }

    //进行选中Tab的处理
    private void selectTab(int i) {
        reset();
        //获取FragmentManager对象
        FragmentManager manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        FragmentTransaction transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
        switch (i) {
            //当选中点击Tab时
            case 0:
                tab1.setBackgroundResource(R.drawable.cj_tab_pressed_bg);
                //如果对应的Fragment没有实例化，则进行实例化，并显示出来
                if (mFrag1 == null) {
                    mFrag1 = new TimeFragment();
                    transaction.add(R.id.id_content, mFrag1);
                } else {
                    //如果对应的Fragment已经实例化，则直接显示出来
                    transaction.show(mFrag1);
                }
                break;
            case 1:
                tab2.setBackgroundResource(R.drawable.cj_tab_pressed_bg);
                if (mFrag2 == null) {
                    mFrag2 = new StatisticsFragment();
                    transaction.add(R.id.id_content, mFrag2);
                } else {
                    transaction.show(mFrag2);
                }
                break;
            case 2:
                tab3.setBackgroundResource(R.drawable.cj_tab_pressed_bg);
                if (mFrag3 == null) {
                    mFrag3 = new RecordFragment();
                    transaction.add(R.id.id_content, mFrag3);
                } else {
                    transaction.show(mFrag3);
                }
                break;

        }
        //提交事务
        transaction.commit();
    }

    private void reset() {
        tab1.setBackgroundResource(R.color.cj_tab_color);
        tab2.setBackgroundResource(R.color.cj_tab_color);
        tab3.setBackgroundResource(R.color.cj_tab_color);
    }


    private void hideFragments(FragmentTransaction transaction) {
        if (mFrag1 != null) {
            transaction.hide(mFrag1);
        }
        if (mFrag2 != null) {
            transaction.hide(mFrag2);
        }
        if (mFrag3 != null) {
            transaction.hide(mFrag3);
        }
    }
}
